package cn.itcast.mq.listener;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
 *SPringAMQP会帮我们自动和MQ建立监听，我们只要指定监听的队列和消息处理的方式
 **/
@Component
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//    }

//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueueMessage(String msg) throws InterruptedException {
//        System.out.println("spring 消费者1接收到消息：【" + msg + "】"+ LocalTime.now());
//        Thread.sleep(20);//指每秒处理五十次
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    public void listenWork1QueueMessage(String msg) throws InterruptedException {
//        System.err.println("spring 消费者2接收到消息：【" + msg + "】"+LocalTime.now());
//        Thread.sleep(20);
//    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanOutQueueMessage1(String msg){
        System.err.println("spring 消费者1接收到消息：【" + msg + "】"+LocalTime.now());
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanOutQueueMessage2(String msg){
        System.err.println("spring 消费者2接收到消息：【" + msg + "】"+LocalTime.now());
    }

    /**
     * 声明交换机itcast.direct
     * 声明队列direct.queue1
     * 声明bindingkey
     * 绑定交换机和队列
     * @param msg
     */
    @RabbitListener(bindings =@QueueBinding(
            value = @Queue("direct.queue1"),
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void bindDirectQueue1(String msg){
        System.out.println("spring 消费者1接收到消息：【" + msg + "】"+LocalTime.now());
    }
    /**
     * 声明交换机itcast.direct
     * 声明队列direct.queue2
     * 声明bindingkey
     * 绑定交换机和队列
     * @param msg
     */
    @RabbitListener(bindings =@QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange(value = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void bindDirectQueue2(String msg){
        System.out.println("spring 消费者2接收到消息：【" + msg + "】"+LocalTime.now());
    }

    /**
     * 声明交换机itcast.topic
     * 声明队列topic.queue1
     * 声明bindingkeyWie通配符
     * 绑定交换机和队列
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="topic.queue1"),
            exchange = @Exchange(name="itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void bindTopicQueue1(String msg){
        System.out.println("spring 消费者1接收到消息：【" + msg + "】"+LocalTime.now());
    }

    /**
     * 声明交换机itcast.topic
     * 声明队列topic.queue2
     * 声明bindingkey为通配符
     * 绑定交换机和队列
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="topic.queue2"),
            exchange = @Exchange(name="itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void bindTopicQueue2(String msg){
        System.out.println("spring 消费者2接收到消息：【" + msg + "】"+LocalTime.now());
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> map){
        System.out.println(map);
    }
}